home *** CD-ROM | disk | FTP | other *** search
/ Aminet 35 / Aminet 35 (2000)(Schatztruhe)[!][Feb 2000].iso / Aminet / gfx / misc / gnuplot-src.lha / gnuplot-3.7.1src / gnuplot-3.7.1.lha / gnuplot-3.7.1 / win / wprinter.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-15  |  13.4 KB  |  508 lines

  1. #ifndef lint Þ4-9$5ŒSid = "$Id: wprinter.c,v 1.1.1.2 1998/04/15 19:23:55 lhecking Exp $";
  2. #endif
  3.  
  4. /* GNUPLOT - win/wprinter.c */
  5. /*[
  6.  * Copyright 1992, 1993, 1998   Maurice Castro, Russell Lang
  7.  *
  8.  * Permission to use, copy, and distribute this software and its
  9.  * documentation for any purpose with or without fee is hereby granted,
  10.  * provided that the above copyright notice appear in all copies and
  11.  * that both that copyright notice and this permission notice appear
  12.  * in supporting documentation.
  13.  *
  14.  * Permission to modify the software is granted, but not the right to
  15.  * distribute the complete modified source code.  Modifications are to
  16.  * be distributed as patches to the released version.  Permission to
  17.  * distribute binaries produced by compiling modified sources is granted,
  18.  * provided you
  19.  *   1. distribute the corresponding source modifications from the
  20.  *    released version in the form of a patch file along with the binaries,
  21.  *   2. add special version identification to distinguish your version
  22.  *    in addition to the base release version number,
  23.  *   3. provide your name and address as the primary contact for the
  24.  *    support of your modified version, and
  25.  *   4. retain our contact information in regard to use of the base
  26.  *    software.
  27.  * Permission to distribute the released version of the source code along
  28.  * with corresponding source modifications in the form of a patch file is
  29.  * granted with same provisions 2 through 4 for binary distributions.
  30.  *
  31.  * This software is provided "as is" without express or implied warranty
  32.  * to the extent permitted by applicable law.
  33. ]*/
  34.  
  35. /*
  36.  * AUTHORS
  37.  * 
  38.  *   Maurice Castro
  39.  *   Russell Lang
  40.  * 
  41.  * Send your comments or suggestions to 
  42.  *  info-gnuplot@dartmouth.edu.
  43.  * This is a mailing list; to join it send a note to 
  44.  *  majordomo@dartmouth.edu.  
  45.  * Send bug reports to
  46.  *  bug-gnuplot@dartmouth.edu.
  47.  */
  48.  
  49. /* Dump a file to the printer */
  50.  
  51. #define STRICT
  52. #include <windows.h>
  53. #include <windowsx.h>
  54. #if WINVER >= 0x030a
  55. #include <commdlg.h>
  56. #endif
  57. #ifdef WIN32
  58. #include <stdio.h>
  59. #include <stdlib.h>
  60. #endif
  61. #ifdef __MSC__
  62. #include <memory.h>
  63. #else
  64. #include <mem.h>
  65. #endif
  66. #include "wgnuplib.h"
  67. #include "wresourc.h"
  68. #include "wcommon.h"
  69.  
  70. LPPRINT prlist = NULL;
  71.  
  72. BOOL CALLBACK WINEXPORT PrintSizeDlgProc(HWND hdlg, UINT wmsg, WPARAM wparam, LPARAM lparam);
  73. BOOL CALLBACK WINEXPORT
  74. PrintSizeDlgProc(HWND hdlg, UINT wmsg, WPARAM wparam, LPARAM lparam)
  75. {
  76.     char buf[8];
  77.     LPPRINT lpr;
  78.     lpr = (LPPRINT)GetWindowLong(GetParent(hdlg), 4);
  79.  
  80.     switch (wmsg) {
  81.         case WM_INITDIALOG:
  82.             wsprintf(buf,"%d",lpr->pdef.x);
  83.             SetDlgItemText(hdlg, PSIZE_DEFX, buf);
  84.             wsprintf(buf,"%d",lpr->pdef.y);
  85.             SetDlgItemText(hdlg, PSIZE_DEFY, buf);
  86.             wsprintf(buf,"%d",lpr->poff.x);
  87.             SetDlgItemText(hdlg, PSIZE_OFFX, buf);
  88.             wsprintf(buf,"%d",lpr->poff.y);
  89.             SetDlgItemText(hdlg, PSIZE_OFFY, buf);
  90.             wsprintf(buf,"%d",lpr->psize.x);
  91.             SetDlgItemText(hdlg, PSIZE_X, buf);
  92.             wsprintf(buf,"%d",lpr->psize.y);
  93.             SetDlgItemText(hdlg, PSIZE_Y, buf);
  94.             CheckDlgButton(hdlg, PSIZE_DEF, TRUE);
  95.             EnableWindow(GetDlgItem(hdlg, PSIZE_X), FALSE);
  96.             EnableWindow(GetDlgItem(hdlg, PSIZE_Y), FALSE);
  97.             return TRUE;
  98.         case WM_COMMAND:
  99.             switch (wparam) {
  100.                 case PSIZE_DEF:
  101.                     EnableWindow(GetDlgItem(hdlg, PSIZE_X), FALSE);
  102.                     EnableWindow(GetDlgItem(hdlg, PSIZE_Y), FALSE);
  103.                     return FALSE;
  104.                 case PSIZE_OTHER:
  105.                     EnableWindow(GetDlgItem(hdlg, PSIZE_X), TRUE);
  106.                     EnableWindow(GetDlgItem(hdlg, PSIZE_Y), TRUE);
  107.                     return FALSE;
  108.                 case IDOK:
  109.                     if (SendDlgItemMessage(hdlg, PSIZE_OTHER, BM_GETCHECK, 0, 0L)) {
  110.                         SendDlgItemMessage(hdlg, PSIZE_X, WM_GETTEXT, 7, (LPARAM)((LPSTR)buf));
  111.                         GetInt(buf, (LPINT)&lpr->psize.x);
  112.                         SendDlgItemMessage(hdlg, PSIZE_Y, WM_GETTEXT, 7, (LPARAM)((LPSTR)buf));
  113.                         GetInt(buf, (LPINT)&lpr->psize.y);
  114.                     }
  115.                     else {
  116.                         lpr->psize.x = lpr->pdef.x;
  117.                         lpr->psize.y = lpr->pdef.y;
  118.                     }
  119.                     SendDlgItemMessage(hdlg, PSIZE_OFFX, WM_GETTEXT, 7, (LPARAM)((LPSTR)buf));
  120.                     GetInt(buf, (LPINT)&lpr->poff.x);
  121.                     SendDlgItemMessage(hdlg, PSIZE_OFFY, WM_GETTEXT, 7, (LPARAM)((LPSTR)buf));
  122.                     GetInt(buf, (LPINT)&lpr->poff.y);
  123.  
  124.                     if (lpr->psize.x <= 0)
  125.                         lpr->psize.x = lpr->pdef.x;
  126.                     if (lpr->psize.y <= 0)
  127.                         lpr->psize.y = lpr->pdef.y;
  128.  
  129.                     EndDialog(hdlg, IDOK);
  130.                     return TRUE;
  131.                 case IDCANCEL:
  132.                     EndDialog(hdlg, IDCANCEL);
  133.                     return TRUE;
  134.             }
  135.             break;
  136.     }
  137.     return FALSE;
  138. }
  139.  
  140.  
  141.  
  142. /* GetWindowLong(hwnd, 4) must be available for use */
  143. BOOL
  144. PrintSize(HDC printer, HWND hwnd, LPRECT lprect)
  145. {
  146. HDC hdc;
  147. DLGPROC lpfnPrintSizeDlgProc ;
  148. BOOL status = FALSE;
  149. PRINT pr;
  150.  
  151.     SetWindowLong(hwnd, 4, (LONG)((LPPRINT)&pr));
  152.     pr.poff.x = 0;
  153.     pr.poff.y = 0;
  154.     pr.psize.x = GetDeviceCaps(printer, HORZSIZE);
  155.     pr.psize.y = GetDeviceCaps(printer, VERTSIZE);
  156.     hdc = GetDC(hwnd);
  157.     GetClientRect(hwnd,lprect);
  158.     pr.pdef.x = MulDiv(lprect->right-lprect->left, 254, 10*GetDeviceCaps(hdc, LOGPIXELSX));
  159.     pr.pdef.y = MulDiv(lprect->bottom-lprect->top, 254, 10*GetDeviceCaps(hdc, LOGPIXELSX));
  160.     ReleaseDC(hwnd,hdc);
  161. #ifdef WIN32
  162.     if (DialogBox (hdllInstance, "PrintSizeDlgBox", hwnd, PrintSizeDlgProc)
  163. #else
  164. #ifdef __DLL__
  165.     lpfnPrintSizeDlgProc = (DLGPROC)GetProcAddress(hdllInstance, "PrintSizeDlgProc");
  166. #else
  167.     lpfnPrintSizeDlgProc = (DLGPROC)MakeProcInstance((FARPROC)PrintSizeDlgProc, hdllInstance);
  168. #endif
  169.     if (DialogBox (hdllInstance, "PrintSizeDlgBox", hwnd, lpfnPrintSizeDlgProc)
  170. #endif
  171.         == IDOK) {
  172.         lprect->left = MulDiv(pr.poff.x*10, GetDeviceCaps(printer, LOGPIXELSX), 254);
  173.         lprect->top = MulDiv(pr.poff.y*10, GetDeviceCaps(printer, LOGPIXELSY), 254);
  174.         lprect->right = lprect->left + MulDiv(pr.psize.x*10, GetDeviceCaps(printer, LOGPIXELSX), 254);
  175.         lprect->bottom = lprect->top + MulDiv(pr.psize.y*10, GetDeviceCaps(printer, LOGPIXELSY), 254);
  176.         status = TRUE;
  177.     }
  178. #ifndef WIN32
  179. #ifndef __DLL__
  180.     FreeProcInstance((FARPROC)lpfnPrintSizeDlgProc);
  181. #endif
  182. #endif
  183.     SetWindowLong(hwnd, 4, (LONG)(0L));
  184.  
  185.     return status;
  186. }
  187.  
  188. #ifdef WIN32
  189. /* Win32 doesn't support OpenJob() etc. so we must use some old code
  190.  * which attempts to sneak the output through a Windows printer driver */
  191. void 
  192. PrintRegister(LPPRINT lpr)
  193. {
  194.     LPPRINT next;
  195.     next = prlist;
  196.     prlist = lpr;
  197.     lpr->next = next;
  198. }
  199.  
  200. LPPRINT
  201. PrintFind(HDC hdc)
  202. {
  203.     LPPRINT this;
  204.     this = prlist;
  205.     while (this && (this->hdcPrn!=hdc)) {
  206.         this = this->next;
  207.     }
  208.     return this;
  209. }
  210.  
  211. void
  212. PrintUnregister(LPPRINT lpr)
  213. {
  214.     LPPRINT this, prev;
  215.     prev = (LPPRINT)NULL;
  216.     this = prlist;
  217.     while (this && (this!=lpr)) {
  218.         prev = this;
  219.         this = this->next;
  220.     }
  221.     if (this && (this == lpr)) {
  222.         /* unhook it */
  223.         if (prev)
  224.             prev->next = this->next;
  225.         else
  226.             prlist = this->next;
  227.     }
  228. }
  229.  
  230.  
  231. /* GetWindowLong(GetParent(hDlg), 4) must be available for use */
  232. BOOL CALLBACK WINEXPORT
  233. PrintDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  234. {
  235.     LPPRINT lpr;
  236.     lpr = (LPPRINT)GetWindowLong(GetParent(hDlg), 4);
  237.  
  238.     switch(message) {
  239.         case WM_INITDIALOG:
  240.             lpr->hDlgPrint = hDlg;
  241.             SetWindowText(hDlg,(LPSTR)lParam);
  242.             EnableMenuItem(GetSystemMenu(hDlg,FALSE),SC_CLOSE,MF_GRAYED);
  243.             return TRUE;
  244.         case WM_COMMAND:
  245.             lpr->bUserAbort = TRUE;
  246.             lpr->hDlgPrint = 0;
  247.             EnableWindow(GetParent(hDlg),TRUE);
  248.             EndDialog(hDlg, FALSE);
  249.             return TRUE;
  250.     }
  251.     return FALSE;
  252. }
  253.  
  254.     
  255. BOOL CALLBACK WINEXPORT
  256. PrintAbortProc(HDC hdcPrn, int code)
  257. {
  258.     MSG msg;
  259.     LPPRINT lpr;
  260.     lpr = PrintFind(hdcPrn);
  261.  
  262.     while (!lpr->bUserAbort && PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
  263.         if (!lpr->hDlgPrint || !IsDialogMessage(lpr->hDlgPrint,&msg)) {
  264.             TranslateMessage(&msg);
  265.             DispatchMessage(&msg);
  266.         }
  267.     }
  268.     return(!lpr->bUserAbort);
  269. }
  270.  
  271.  
  272. /* GetWindowLong(hwnd, 4) must be available for use */
  273. void WDPROC
  274. DumpPrinter(HWND hwnd, LPSTR szAppName, LPSTR szFileName)
  275. {
  276. HDC printer;
  277. PRINTDLG pd;
  278. PRINT pr;
  279. DOCINFO di;
  280. char *buf;
  281. WORD *bufcount;
  282. int count;
  283. FILE *f;
  284. long lsize;
  285. long ldone;
  286. char pcdone[10];
  287.  
  288.     memset(&pd, 0, sizeof(PRINTDLG));
  289.     pd.lStructSize = sizeof(PRINTDLG);
  290.     pd.hwndOwner = hwnd;
  291.     pd.Flags = PD_PRINTSETUP | PD_RETURNDC;
  292.  
  293.     if ((f = fopen(szFileName, "rb")) == (FILE *)NULL)
  294.        return;
  295.     fseek(f, 0L, SEEK_END);
  296.     lsize = ftell(f);
  297.     if (lsize <= 0)
  298.         lsize = 1;
  299.     fseek(f, 0L, SEEK_SET);
  300.     ldone = 0;
  301.  
  302.     if (PrintDlg(&pd)) {
  303.     printer = pd.hDC;
  304.     if (printer != (HDC)NULL) {
  305.       pr.hdcPrn = printer;
  306.       SetWindowLong(hwnd, 4, (LONG)((LPPRINT)&pr));
  307.       PrintRegister((LPPRINT)&pr);
  308.       if ( (buf = malloc(4096+2)) != (char *)NULL ) {
  309.             bufcount = (WORD *)buf;
  310.         EnableWindow(hwnd,FALSE);
  311.         pr.bUserAbort = FALSE;
  312. /* is parent set correctly */
  313.         pr.hDlgPrint = CreateDialogParam(hdllInstance,"CancelDlgBox",hwnd,PrintDlgProc,(LPARAM)szAppName);
  314.         SetAbortProc(printer, PrintAbortProc);
  315.         di.cbSize = sizeof(DOCINFO);
  316.         di.lpszDocName = szAppName;
  317.         di.lpszOutput = NULL;
  318.         if (StartDoc(printer, &di) > 0) {
  319.             while ( pr.hDlgPrint && !pr.bUserAbort 
  320.             && (count = fread(buf+2, 1, 4096, f)) != 0 ) {
  321.             *bufcount = count;
  322.             Escape(printer, PASSTHROUGH, count+2, (LPSTR)buf, NULL);
  323.                 ldone += count;
  324.                 sprintf(pcdone, "%d%% done", (int)(ldone * 100 / lsize));
  325.                 SetWindowText(GetDlgItem(pr.hDlgPrint, CANCEL_PCDONE), pcdone);
  326.             if (pr.bUserAbort) 
  327.                 AbortDoc(printer);
  328.             else
  329.                 EndDoc(printer);
  330.             }
  331.             if (!pr.bUserAbort) {
  332.             EnableWindow(hwnd,TRUE);
  333.             DestroyWindow(pr.hDlgPrint);
  334.             }
  335.             free(buf);
  336.         }
  337.       }
  338.       DeleteDC(printer);
  339.       SetWindowLong(hwnd, 4, (LONG)(0L));
  340.       PrintUnregister((LPPRINT)&pr);
  341.     }
  342.     }
  343.     fclose(f);
  344. }
  345.  
  346.  
  347. #else  /* !WIN32 */
  348. /* documented in Device Driver Adaptation Guide */
  349. /* Prototypes taken from print.h */
  350. DECLARE_HANDLE(HPJOB);
  351.  
  352. HPJOB   WINAPI OpenJob(LPSTR, LPSTR, HPJOB);
  353. int     WINAPI StartSpoolPage(HPJOB);
  354. int     WINAPI EndSpoolPage(HPJOB);
  355. int     WINAPI WriteSpool(HPJOB, LPSTR, int);
  356. int     WINAPI CloseJob(HPJOB);
  357. int     WINAPI DeleteJob(HPJOB, int);
  358. int     WINAPI WriteDialog(HPJOB, LPSTR, int);
  359. int     WINAPI DeleteSpoolPage(HPJOB);
  360.  
  361. /* Modeless dialog box - Cancel printing */
  362. BOOL CALLBACK WINEXPORT
  363. CancelDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  364. {
  365.     switch(message) {
  366.     case WM_INITDIALOG:
  367.         SetWindowText(hDlg,(LPSTR)lParam);
  368.         return TRUE;
  369.     case WM_COMMAND:
  370.         switch(LOWORD(wParam)) {
  371.         case IDCANCEL:
  372.             DestroyWindow(hDlg);
  373.             return TRUE;
  374.         }
  375.     }
  376.     return FALSE;
  377. }
  378.  
  379.  
  380.  
  381. /* Dialog box to select printer port */
  382. BOOL CALLBACK WINEXPORT
  383. SpoolDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  384. {
  385. LPSTR entry;
  386.     switch(message) {
  387.     case WM_INITDIALOG:
  388.         entry = (LPSTR)lParam;
  389.         while (*entry) {
  390.         SendDlgItemMessage(hDlg, SPOOL_PORT, LB_ADDSTRING, 0, (LPARAM)entry);
  391.         entry += lstrlen(entry)+1;
  392.         }
  393.         SendDlgItemMessage(hDlg, SPOOL_PORT, LB_SETCURSEL, 0, (LPARAM)0);
  394.         return TRUE;
  395.     case WM_COMMAND:
  396.         switch(LOWORD(wParam)) {
  397.         case SPOOL_PORT:
  398.             if (HIWORD(lParam) == LBN_DBLCLK)
  399.             PostMessage(hDlg, WM_COMMAND, IDOK, 0L);
  400.             return FALSE;
  401.         case IDOK:
  402.             EndDialog(hDlg, 1+(int)SendDlgItemMessage(hDlg, SPOOL_PORT, LB_GETCURSEL, 0, 0L));
  403.             return TRUE;
  404.         case IDCANCEL:
  405.             EndDialog(hDlg, 0);
  406.             return TRUE;
  407.         }
  408.     }
  409.     return FALSE;
  410. }
  411.  
  412. /* Print File to port */
  413. void WDPROC
  414. DumpPrinter(HWND hwnd, LPSTR szAppName, LPSTR szFileName)
  415. {
  416. #define PRINT_BUF_SIZE 4096
  417. char *buffer;
  418. char *portname;
  419. int i, iport;
  420. DLGPROC lpfnSpoolProc;
  421. HPJOB hJob;
  422. UINT count;
  423. HFILE hf;
  424. int error = FALSE;
  425. DLGPROC lpfnCancelProc;
  426. long lsize;
  427. long ldone;
  428. char pcdone[10];
  429. MSG msg;
  430. HWND hDlgModeless;
  431.  
  432.     if ((buffer = LocalAllocPtr(LHND, PRINT_BUF_SIZE)) == (char *)NULL)
  433.         return;
  434.     /* get list of ports */
  435.     GetProfileString("ports", NULL, "", buffer, PRINT_BUF_SIZE);
  436.     /* select a port */
  437.     lpfnSpoolProc = (DLGPROC)MakeProcInstance((FARPROC)SpoolDlgProc, hdllInstance);
  438.     iport = DialogBoxParam(hdllInstance, "SpoolDlgBox", hwnd, lpfnSpoolProc, (LPARAM)buffer);
  439.     FreeProcInstance((FARPROC)lpfnSpoolProc);
  440.     if (!iport) {
  441.         LocalFreePtr((void NEAR *)buffer);
  442.         return;
  443.     }
  444.     portname = buffer;
  445.     for (i=1; i<iport && lstrlen(portname)!=0; i++)
  446.         portname += lstrlen(portname)+1;
  447.     
  448.     /* open file and get length */
  449.     hf = _lopen(szFileName, OF_READ);
  450.     if (hf == HFILE_ERROR) {
  451.         LocalFreePtr((void NEAR *)buffer);
  452.         return;
  453.     }
  454.     lsize = _llseek(hf, 0L, 2);
  455.     (void)_llseek(hf, 0L, 0);
  456.     if (lsize <= 0)
  457.         lsize = 1;
  458.  
  459.     hJob = OpenJob(portname, szFileName, (HDC)NULL);
  460.     switch ((int)hJob) {
  461.         case SP_APPABORT:
  462.         case SP_ERROR:
  463.         case SP_OUTOFDISK:
  464.         case SP_OUTOFMEMORY:
  465.         case SP_USERABORT:
  466.             _lclose(hf);
  467.         LocalFreePtr((void NEAR *)buffer);
  468.             return;
  469.     }
  470.     if (StartSpoolPage(hJob) < 0)
  471.         error = TRUE;
  472.  
  473.     ldone = 0;
  474.     lpfnCancelProc = (DLGPROC)MakeProcInstance((FARPROC)CancelDlgProc, hdllInstance);
  475.     hDlgModeless = CreateDialogParam(hdllInstance, "CancelDlgBox", hwnd, lpfnCancelProc, (LPARAM)szAppName);
  476.  
  477.     while (!error && hDlgModeless && IsWindow(hDlgModeless)
  478.           && ((count = _lread(hf, buffer, PRINT_BUF_SIZE))!= 0) ) {
  479.         wsprintf(pcdone, "%d%% done", (int)(ldone * 100 / lsize));
  480.         SetWindowText(GetDlgItem(hDlgModeless, CANCEL_PCDONE), pcdone);
  481.         if (WriteSpool(hJob, buffer, count) < 0)
  482.         error = TRUE;
  483.         ldone += count;
  484.         while (IsWindow(hDlgModeless) && PeekMessage(&msg, hDlgModeless, 0, 0, PM_REMOVE)) {
  485.             if (!IsDialogMessage(hDlgModeless, &msg)) {
  486.             TranslateMessage(&msg);
  487.             DispatchMessage(&msg);
  488.         }
  489.         }
  490.     }
  491.     LocalFreePtr((void NEAR *)buffer);
  492.     _lclose(hf);
  493.  
  494.     if (!hDlgModeless || !IsWindow(hDlgModeless))
  495.         error=TRUE;
  496.     if (IsWindow(hDlgModeless))
  497.         DestroyWindow(hDlgModeless);
  498.     hDlgModeless = 0;
  499.     FreeProcInstance((FARPROC)lpfnCancelProc);
  500.     EndSpoolPage(hJob);
  501.     if (error)
  502.         DeleteJob(hJob, 0);
  503.     else
  504.         CloseJob(hJob);
  505. }
  506. #endif /* !WIN32 */
  507.  
  508.